/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package transaction_manager_client;

import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.*;
import javax.xml.rpc.encoding.XMLType;
import ws_client.ParameterSpecification;
import ws_client.WSAccessor;

/**
 *
 * @author iMarshall
 */
public class TransactionManagerAccessor extends WSAccessor
{
    public TransactionManagerAccessor()
    {
        super();
    }

    // NOTE: provided endpoint is without ?wsdl appended
    // eg: http://localhost:8080/TransactionManager-war/TransactionManagerWSService
    public TransactionManagerAccessor(String endpoint)
    {
        this();
        this.endpoint = endpoint;
        serviceName = new QName(endpoint + "?wsdl", "TransactionManagerWSService");
        namespace = "http://transaction_manager_ws/";
    }

    public String openTransaction()
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps = { };
        Object[] args = { };

        Object returnValue =
            callOperation("openTransaction", ps, args, XMLType.XSD_STRING);

        return returnValue.toString();
    }

    public boolean closeTransaction(String tID)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "tID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object args[] = { tID };

        Object returnValue =
            callOperation("closeTransaction", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }

    public boolean abortTransaction(String tID)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "tID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object args[] = { tID };

        Object returnValue =
            callOperation("abortTransaction", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }

    public boolean join(String tID, String pID)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "tID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN),

            new ParameterSpecification(
                "pID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object[] args = { tID, pID };

        Object returnValue =
            callOperation("join", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }

    // NOTE: this should not be called
    public boolean haveCommitted(String tID, String pID)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "tID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN),

            new ParameterSpecification(
                "pID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object[] args = { tID, pID };

        Object returnValue =
            callOperation("haveCommitted", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }
}
